package com.whmskj.xjlhsc.pc.productionreport.task.efficiency;

import com.whmskj.xjlhsc.pc.productionreport.task.mapper.ProductionMapper;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.EfficiencyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-16 19:46
 */

@Slf4j
@Component
@EnableScheduling
public class EfficiencyTask {

    @Autowired
    private ProductionMapper productionMapper;

    //每天早上10点执行
    @Scheduled(cron = "0 0 10 * * ?")
    //每隔5秒执行
//    @Scheduled(cron = "0/5 * * * * ?")
    public void efficiencyTask() {
        List<EfficiencyVO> efficiencyData = productionMapper.getEfficiencyData();
        for (EfficiencyVO efficiencyDatum : efficiencyData) {
            //所有小数四舍五入保留两位小数
            efficiencyDatum.setEfficiency(Math.round(efficiencyDatum.getEfficiency() * 100.0) / 100.0f);
            efficiencyDatum.setProductionKg(Math.round(efficiencyDatum.getProductionKg() * 100.0) / 100.0f);
            efficiencyDatum.setProductionKm(Math.round(efficiencyDatum.getProductionKm() * 100.0) / 100.0f);
            efficiencyDatum.setProductionPerShift(Math.round(efficiencyDatum.getProductionPerShift() * 100.0) / 100.0f);
            efficiencyDatum.setEnergyConsumption(Math.round(efficiencyDatum.getEnergyConsumption() * 100.0) / 100.0f);
            efficiencyDatum.setEnergyConsumptionPerShift(Math.round(efficiencyDatum.getEnergyConsumptionPerShift() * 100.0) / 100.0f);
        }
        if (productionMapper.getDayProductionDataByEfficiency(LocalDate.now().minusDays(1).toString()) == 0) {
            productionMapper.insertEfficiencyData(efficiencyData);
        }
    }
}
